package com.hk.springdatajpa;

import com.hk.springdatajpa.dao.RoleRepository;
import com.hk.springdatajpa.dao.UserRepository;
import com.hk.springdatajpa.entity.Role;
import com.hk.springdatajpa.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author by 何坤
 * @Classname JpaTest03
 * @Description TODO
 * @Date 2019/9/27 15:21
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class JpaTest03 {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RoleRepository roleRepository;

    /**
     * 测试JPA的多对多的添加
     * @param
     * @return void
     * @date 2019/9/27 15:22
     * @author 何坤
     */
    @Test
    @Transactional
    @Rollback(false)
    public void testAdd(){
        User user = new User();
        user.setName("何坤");
        user.setGender(1);
        user.setPhone("13281896139");

        Role role = new Role();
        role.setRole("学生");

        user.getRoles().add(role);

        userRepository.save(user);
        roleRepository.save(role);
    }
}
